package com.hd.tiyu.yuejian.Activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.hd.tiyu.yuejian.Model.UserModel;
import com.hd.tiyu.yuejian.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GeRenUpdateActivity extends AppCompatActivity {
    private ImageView back;
    private EditText user_name,user_pass;
    private Button bt_uname,bt_pass;
    private SharedPreferences sp;
    private String userId;
    String name;
    String password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ge_ren_update);
        initViews();
        initData();
        initEvens();
        updateUname();
        updatePass();
    }

    private void updateUname() {
        bt_uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                name=user_name.getText().toString();
                if (name.equals("")) {
                    Toast.makeText(GeRenUpdateActivity.this, "输入的用户名不能为空", Toast.LENGTH_LONG).show();
                }else {
//                    UserModel model=new UserModel();
//                    Call call= model.getUserChangeName(userId,name);
                    UserModel userModel=new UserModel();
                    Call<Integer>call=userModel.UpdateUname(userId,name);
                    call.enqueue(new Callback<Integer>() {
                        @Override
                        public void onResponse(Call<Integer> call, Response<Integer> response) {
                            if (response.body()==1){
                                Toast.makeText(GeRenUpdateActivity.this,"用户名修改成功",Toast.LENGTH_LONG).show();
                            }else {
                                Toast.makeText(GeRenUpdateActivity.this,"用户名修改失败",Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Integer> call, Throwable t) {

                        }
                    });

                }
            }
        });
    }

    private void updatePass() {
        bt_pass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                password=user_pass.getText().toString();
                if (password.equals("")){
                    Toast.makeText(GeRenUpdateActivity.this,"输入的密码不能为空", Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(GeRenUpdateActivity.this,"密码改为："+password,Toast.LENGTH_LONG).show();
                    UserModel userModel=new UserModel();
                    Call<Integer>call=userModel.UpdatePass(userId,password);
                    call.enqueue(new Callback<Integer>() {
                        @Override
                        public void onResponse(Call<Integer> call, Response<Integer> response) {
                            Toast.makeText(GeRenUpdateActivity.this,response.body().toString(),Toast.LENGTH_LONG).show();
                            if (response.body()==1){
                                Toast.makeText(GeRenUpdateActivity.this,"密码修改成功",Toast.LENGTH_LONG).show();
                            }else {
                                Toast.makeText(GeRenUpdateActivity.this,"密码修改失败",Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Integer> call, Throwable t) {
                            Toast.makeText(GeRenUpdateActivity.this,"----",Toast.LENGTH_LONG).show();


                        }
                    });
                }

            }
        });

    }

    private void initViews() {
        back=(ImageView)findViewById(R.id.img_back);
        user_name=(EditText)findViewById(R.id.Tex_uname);
        user_pass=(EditText)findViewById(R.id.Tex_pass);
        bt_uname=(Button)findViewById(R.id.update_uname);
        bt_pass=(Button)findViewById(R.id.update_pass);

        sp=getSharedPreferences("loginUser", Context.MODE_PRIVATE);
        userId=sp.getString("user_id","");
    }
    private void initData() {
    }
    private void initEvens() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


}
